﻿using AutoUpdateMgr.Mgr;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoUpdateMgr
{
    public partial class FormFileUpload : Form
    {
        public FormFileUpload()
        {
            InitializeComponent();
        }

        public AutoUpdate.Model.Model.fileTransfer.fs_version currentVersion { set; get; } = null;

        private AutoUpdate.Model.Model.fileTransfer.fs_inode currentNode { set; get; } = null;

        private AutoUpdate.Model.Model.fileTransfer.fs_inodeContent currentNodeContent { set; get; } = null;

        private Queue<AutoUpdate.Model.Model.fileTransfer.fs_inode> nodeQueue { set; get; } = new Queue<AutoUpdate.Model.Model.fileTransfer.fs_inode>();

        private long fileOffset { set; get; } = 0;

        private void FormFileUpload_Load(object sender, EventArgs e)
        {
            if (this.currentVersion == null)

                return;

            if (this.currentVersion.files.Count == 0)

                return;

            this.progressBar2.Maximum = this.currentVersion.files.Count;

            //UploadMgr.getInstance().upload(this.currentVersion, this, this.currentVersion.files);

            foreach (var item in this.currentVersion.files)
            {
                this.nodeQueue.Enqueue(item);
            }

            new Task(() =>
            {
                this.currentNode = this.nodeQueue.Dequeue();

                this.fileOffset = 0;

                this.progressBar1.Maximum = (int)this.currentNode.length;

                this.progressBar1.Value = (int)this.fileOffset;

                this.label1.Text = "正在上传=>" + this.currentNode.name;

                this.uploadFile();

            }).Start();
        }


        internal void changeCurrentFileName(string msg)
        {
            if (this != null)
            {
                this.Invoke(new Action(() =>
               {
                   this.label1.Text = msg;
               }));
            }
        }
        internal void setFileUploadCount(int value)
        {
            if (this != null)
            {
                this.Invoke(new Action(() =>
                {
                    this.progressBar2.Maximum = value;
                }));
            }
        }

        internal void setFileUploadIndex(int value)
        {
            if (this != null)
            {
                this.Invoke(new Action(() =>
                {
                    this.progressBar2.Value = value;
                }));
            }
        }
        internal void setFileUploadLength(int value)
        {
            if (this != null)
            {
                this.Invoke(new Action(() =>
                {
                    this.progressBar1.Maximum = value;
                }));
            }
        }
        internal void setFileUploadOffset(int value)
        {
            if (this != null)
            {
                this.Invoke(new Action(() =>
                {
                    this.progressBar1.Value = value;
                }));
            }
        }


        private void uploadFile()
        {
            if (this.currentNode == null)

                return;

            int fileBlockLength = 512 * 1024;

            int length = 0;

            byte[] fileBuffer = null;

            try
            {
                fileBuffer = AutoUpdate.Utility.Helper.FileHelper.fileRead(this.currentNode.realPath, this.fileOffset, fileBlockLength, out length);
            }
            catch (Exception ex)
            {
                if (this.nodeQueue.Count > 0)
                {
                    this.currentNode = this.nodeQueue.Dequeue();

                    this.fileOffset = 0;

                    this.progressBar2.Value = this.progressBar2.Maximum - this.nodeQueue.Count;

                    if (this.currentNode != null)

                        this.label1.Text = "正在上传=>" + this.currentNode.name;

                    else
                        this.label1.Text = "上传完毕";

                    this.uploadFile();

                    return;
                }
                else
                {
                    AutoUpdate.Model.Packet.RequestPacket request1 = new AutoUpdate.Model.Packet.RequestPacket();

                    request1.CommandText = "updateVersion";

                    request1.MessageContent = AutoUpdate.Utility.Helper.JsonHelper.getString(this.currentVersion);

                    ClientMgr.getInstance().client.sendMessage(AutoUpdate.Utility.Helper.JsonHelper.getString(request1));

                    this.currentNode = null;

                    return;
                }


            }



            AutoUpdate.Model.Model.fileTransfer.fs_inodeContent nodeModel = new AutoUpdate.Model.Model.fileTransfer.fs_inodeContent();

            nodeModel.length = length;

            nodeModel.md5 = this.currentNode.md5;

            nodeModel.offset = this.fileOffset;

            nodeModel.version = this.currentVersion.version;

            nodeModel.fileLength = this.currentNode.length;

            nodeModel.contents = fileBuffer;

            AutoUpdate.Model.Packet.RequestPacket request = new AutoUpdate.Model.Packet.RequestPacket();

            request.CommandText = "uploadFile";

            request.MessageContent = AutoUpdate.Utility.Helper.JsonHelper.getString(nodeModel);

            ActionMgr.getInstance().addAction(request.PacketID, new Action<AutoUpdate.Model.Packet.Packet>((AutoUpdate.Model.Packet.Packet packet) =>
            {
                Console.WriteLine("Thread.Sleep(50000);");

                AutoUpdate.Model.Model.fileTransfer.fs_inodeContent inodeModel = AutoUpdate.Utility.Helper.JsonHelper.getObject<AutoUpdate.Model.Model.fileTransfer.fs_inodeContent>(packet.MessageContent);

                if (inodeModel != null)
                {
                    this.fileOffset += inodeModel.length;

                    this.progressBar1.Maximum = (int)this.currentNode.length;

                    this.progressBar1.Value = (int)this.fileOffset;

                    if (fileOffset == inodeModel.fileLength)
                    {
                        Console.WriteLine("Thread.Sleep(5000011);");

                        if (this.nodeQueue.Count > 0)

                            this.currentNode = this.nodeQueue.Dequeue();

                        else
                        {
                            Console.WriteLine("Thread.Sleep(500001122);");
                            AutoUpdate.Model.Packet.RequestPacket request1 = new AutoUpdate.Model.Packet.RequestPacket();

                            request1.CommandText = "updateVersion";

                            request1.MessageContent = AutoUpdate.Utility.Helper.JsonHelper.getString(this.currentVersion);

                            Console.WriteLine(request1.MessageContent.Length);

                            ClientMgr.getInstance().client.sendMessage(AutoUpdate.Utility.Helper.JsonHelper.getString(request1));

                            this.currentNode = null;
                        }

                        this.fileOffset = 0;

                        this.progressBar2.Value = this.progressBar2.Maximum - this.nodeQueue.Count;

                        if (this.currentNode != null)

                            this.label1.Text = "正在上传=>" + this.currentNode.name;

                        else
                            this.label1.Text = "上传完毕";
                    }

                    this.uploadFile();
                }

            }));

            ClientMgr.getInstance().client.sendMessage(AutoUpdate.Utility.Helper.JsonHelper.getString(request));
        }










    }
}
